<template>
    <div id="main-container" class="main-container" :class="collapse?'position-collapse-left':'position-left'">
      <div class="tab-container">
        <el-tabs class="tabs" :class="collapse?'position-collapse-left':'position-left'"
                 v-model="activeTabName" type="card" :closable="true" @tab-click="selectedTabHandle" @tab-remove="removeTabHandle">
          <el-dropdown class="tab-tools" :show-timeout="0" trigger="hover">
            <div style="font-size:20px;width:50px;"><i class="el-icon-arrow-down"></i></div>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="tabsCloseCurrentHandle">关闭当前标签</el-dropdown-item>
              <el-dropdown-item @click.native="tabsCloseOtherHandle">关闭其它标签</el-dropdown-item>
              <el-dropdown-item @click.native="tabsCloseAllHandle">关闭全部标签</el-dropdown-item>
              <el-dropdown-item @click.native="tabsRefreshCurrentHandle">刷新当前标签</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <el-tab-pane v-for="item in mainTabs"
                       :key="item.name" :label="item.title" :name="item.name">
            <span slot="label"><i :class="item.icon"></i> {{item.title}} </span>
          </el-tab-pane>
        </el-tabs>
      </div>
      <div class="main-content">
        <keep-alive>
          <transition name="fade" mode="out-in">
            <router-view></router-view>
          </transition>
        </keep-alive>
      </div>
    </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
    name: 'RightContent',
    data() {
        return {
        }
    },
    computed: {
        ...mapState({
            themeColor: state => state.app.themeColor,
            collapse: state => state.app.collapse
        }),
        mainTabs: {
            get() {
                return this.$store.state.tab.mainTabs
            },
            set(val) {
                this.$store.commit('updateMainTabs', val)
            }
        },
        activeTabName: {
            get() { return this.$store.state.tab.activeTabName },
            set(val) { this.$store.commit('updateActiveTabName', val) }
        }
    },
    watch: {
    },
    methods: {
    // 选中tab
        selectedTabHandle(tab) {
            tab = this.mainTabs.filter(item => item.name === tab.name)
            if (tab && tab.length >= 1) {
                this.$router.push({ name: tab[0].name })
            }
        },
        // 删除tab
        removeTabHandle(tabName) {
            this.mainTabs = this.mainTabs.filter(item => item.name !== tabName)
            if (this.mainTabs.length >= 1) {
                if (tabName === this.activeTabName) {
                    this.$router.push({ name: this.mainTabs[this.mainTabs.length - 1].name }, () => { this.activeTabName = this.$router.name })
                }
            } else {
                this.$router.push(this.global.homeUrl)
            }
        },
        // 关闭当前标签
        tabsCloseCurrentHandle() {
            this.removeTabHandle(this.activeTabName)
        },
        // 关闭其他标签
        tabsCloseOtherHandle() {
            this.mainTabs = this.mainTabs.filter(item => item.name !== this.activeTabName)
        },
        // 关闭全部标签
        tabsCloseAllHandle() {
            this.mainTabs = []
            this.$router.push(this.global.homeUrl)
        },
        // 刷新当前标签
        tabsRefreshCurrentHandle() {
            const temp = this.activeTabName
            this.removeTabHandle(temp)
            this.$nextTick(() => {
                this.$router.push({ name: temp })
            })
        }
    }
}
</script>

<style scoped>
  .main-container{
    position: absolute;
    top: 60px;
    right: 0;
    bottom: 0;
    padding: 0 5px 5px;
  }
  .tabs{
    cursor: pointer;
    position: fixed;
    top: 60px;
    right: 50px;
    padding-left: 0;
    padding-right: 2px;
    z-index: 1020;
    height: 40px;
    line-height: 40px;
    font-size: 14px;
    background: rgb(255, 255, 255);
    border-color: rgba(200, 206, 206, 0.5);
    border-bottom-width: 1px;
    border-bottom-style: solid;
  }
  .tab-tools {
    cursor: pointer;
    position: fixed;
    top: 60px;
    right: 0;
    z-index: 1020;
    height: 40px;
    padding: 0 10px;
    font-size: 14px;
    line-height: 40px;
    border-color: rgba(200, 206, 206, 0.5);
    border-left-width: 1px;
    border-left-style: solid;
    border-bottom-width: 1px;
    border-bottom-style: solid;
    background: rgba(255, 255, 255, 1);
  }
  .tab-tools:hover {
    cursor: pointer;
    background: rgba(200, 206, 206, 1);
  }
  .main-content {
    position: absolute;
    top: 45px;
    left: 5px;
    right: 5px;
    bottom: 5px;
    padding: 5px;
  }
  .position-left {
    left: 200px;
  }
  .position-collapse-left {
    left: 65px;
  }
</style>
